from celery import Celery
from app.core.config import settings
from app.core.logging import get_logger

# 配置loguru日志
logger = get_logger(__name__)

def make_celery(app_name: str = "celery_app"):
    """创建Celery实例"""
    # 创建Celery应用
    celery_app = Celery(app_name)
    
    # 配置Celery
    celery_app.conf.update(
        broker_url=settings.CELERY_BROKER_URL or "redis://localhost:6379/1",
        result_backend=settings.CELERY_RESULT_BACKEND or "redis://localhost:6379/2",
        task_serializer="json",
        accept_content=["json"],
        result_serializer="json",
        timezone="Asia/Shanghai",
        enable_utc=True,
        worker_prefetch_multiplier=1,
        task_acks_late=True,
        worker_max_tasks_per_child=1000,
    )
    
    return celery_app

# 创建Celery实例
celery_app = make_celery()

if __name__ == "__main__":
    celery_app.start()